package com.spring.test.eureka;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@EnableEurekaServer
@SpringBootApplication
@RestController
public class EurekaServerApplication {
    @Autowired
    private DiscoveryClient client;
    
    @GetMapping("clients")
    public String clients(){
        List<ServiceInstance> list = client.getInstances("service-hi");
        List<String> names = client.getServices();
        ServiceInstance ins = client.getLocalServiceInstance();
        StringBuilder sb = new StringBuilder("hello:");
        sb.append(ins.getHost() + "--" + ins.getPort() + "--" + ins.getServiceId());
        
        names.forEach(name ->{
            sb.append(name);
        });
        
        list.forEach(service -> {
            sb.append(service.getHost() + ":" + service.getPort() + ":" + service.getServiceId()).append("<br>");
        });
        return sb.toString();
    }
    
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
